<template>
  <div class="app-container">
    <el-form :inline="true" ref="searchForm" :model="listQuery" class="demo-form-inline">
      <el-form-item>
        <el-button class="btn-add" @click="handleAddProductCate" size="small"> 添加 </el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table ref="productCateTable" height="100%" style="width: 100%" :data="list" v-loading="listLoading" border>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="分类名称" minWidth="150" show-overflow-tooltip align="center">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="级别" width="100" align="center">
          <template #default="scope">{{ LEVEL_DICT[scope.row.level] }}</template>
        </el-table-column>
        <el-table-column label="商品数量" width="100" align="center">
          <template #default="scope">{{ scope.row.productCount }}</template>
        </el-table-column>
        <el-table-column label="数量单位" width="100" align="center">
          <template #default="scope">{{ scope.row.productUnit }}</template>
        </el-table-column>
        <el-table-column label="导航栏" width="100" align="center">
          <template #default="scope">
            <el-switch @change="handleNavStatusChange(scope.row)" :active-value="1" :inactive-value="0" v-model="scope.row.navStatus">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否显示" width="100" align="center">
          <template #default="scope">
            <el-switch @change="handleShowStatusChange(scope.row)" :active-value="1" :inactive-value="0" v-model="scope.row.showStatus">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template #default="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="设置" width="160" align="center">
          <template #default="scope">
            <el-button link size="mini" type="primary" :disabled="scope.row.level" @click="handleShowNextLevel(scope.row)">
              查看下级
            </el-button>
            <el-button link size="mini" type="info" @click="handleTransferProduct">转移商品 </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <el-button link size="mini" type="primary" @click="handleUpdate(scope.row)">编辑 </el-button>
            <el-button link size="mini" type="danger" @click="handleDelete(scope.row)">删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        v-model:current-page="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { fetchList, deleteProductCate, updateShowStatus, updateNavStatus } from '@/api/productCate'
import type { ProductCate } from '@/models/productCate'
import type { PageDto } from '@/models/request'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { LEVEL_DICT } from '@/constant'
defineOptions({ name: 'ProductCateList' })

const route = useRoute()
const router = useRouter()
const list = ref<ProductCate[]>([])
const total = ref(0)
const listLoading = ref(true)
const parentId = ref(0)
const listQuery = ref<PageDto>({ pageNum: 1, pageSize: 10 })
resetParentId()
getList()
watch(route, () => {
  resetParentId()
  getList()
})
function resetParentId() {
  listQuery.value.pageNum = 1
  if (route.query.parentId != null) {
    parentId.value = Number(route.query.parentId)
  } else {
    parentId.value = 0
  }
}
function handleAddProductCate() {
  router.push('/pms/productCate/upsert')
}
async function getList() {
  listLoading.value = true
  const response = await fetchList(parentId.value, listQuery.value)
  listLoading.value = false
  list.value = response.data.data.list
  total.value = response.data.data.total
}
function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}
function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}
async function handleNavStatusChange(row: ProductCate) {
  const data = { ids: row.id + '', navStatus: row.navStatus }
  await updateNavStatus(data)
  ElMessage.success('修改成功')
}
async function handleShowStatusChange(row: ProductCate) {
  const data = { ids: row.id + '', showStatus: row.showStatus }
  await updateShowStatus(data)
  ElMessage.success('修改成功')
}
function handleShowNextLevel(row: ProductCate) {
  router.push({ path: '/pms/productCate', query: { parentId: row.id } })
}
function handleTransferProduct() {
  console.log('handleAddProductCate')
}
function handleUpdate(row: ProductCate) {
  router.push({ path: '/pms/productCate/upsert', query: { id: row.id } })
}
async function handleDelete(row: ProductCate) {
  await ElMessageBox.confirm('是否要删除该品牌', '提示', { type: 'warning' })
  await deleteProductCate(row.id as number)
  ElMessage.success('删除成功！')
  getList()
}
</script>

<style scoped></style>
